<?php

namespace App\Models;

use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\Models\AdminUser
 *
 * @property \Illuminate\Support\Carbon|null                                                      $created_at
 * @property int                                                                                  $enable   啟用狀態
 * @property int                                                                                  $id
 * @property string                                                                               $name     名字
 * @property string                                                                               $nickname 暱稱
 * @property string                                                                               $password 密碼
 * @property string|null                                                                          $remember_token
 * @property int                                                                                  $staff_id 員工編號
 * @property \Illuminate\Support\Carbon|null                                                      $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[]       $roles
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser permission($permissions)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser role($roles)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereEnable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereStaffId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\AdminUser whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class AdminUser extends Authenticatable
{
    use HasRoles, Notifiable;

    const ENABLE_TRUE  = 1;
    const ENABLE_FALSE = 0;

    protected     $table      = 'admins';
    protected     $guard_name = 'web';
    protected     $fillable   = ['staff_id', 'password', 'name', 'nickname', 'avatar', 'enable', 'call_id', 'email'];
    protected     $hidden     = ['password'];
    public static $enableMap  = [
        self::ENABLE_TRUE  => '啟用',
        self::ENABLE_FALSE => '停用',
    ];

    public function routeNotificationFor($driver, $notification = '')
    {
        switch ($driver) {
            // case 'sms':
            //     return $this->mobile;
            case 'rtx':
                return $this->staff_id;
            case 'mail':
                return $this->email;
            case 'im':
                return $this->staff_id;
        }
        return false;
    }

    public function getEnableNameAttribute()
    {
        return self::$enableMap[$this->enable ?? 0] ?? '';
    }

    public function getRoleNamesAttribute()
    {
        return $this->getRoleNames()->implode(',');
    }

    /**
     * 是否對指定路由有權限
     * @param string $route
     * @return bool
     * @throws \Exception
     */
    public function hasPermissionForRoute($route = '')
    {
        $hasRoutes = [];

        $this->getAllPermissions()->each(function ($permission) use (&$hasRoutes) {
            $hasRoutes = array_merge($hasRoutes, explode(',', $permission->route));
        });

        $hasRoutes = array_unique($hasRoutes);

        return in_array('*', $hasRoutes) || in_array($route, $hasRoutes);
    }
}
